package com.dds.web.controller.union;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dds.common.utils.ServletUtils;
import com.dds.framework.web.service.TokenService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.dds.common.annotation.Log;
import com.dds.common.core.controller.BaseController;
import com.dds.common.core.domain.AjaxResult;
import com.dds.common.enums.BusinessType;
import com.dds.system.domain.union.UnionShopQrcode;
import com.dds.system.service.union.IUnionShopQrcodeService;
import com.dds.common.utils.poi.ExcelUtil;
import com.dds.common.core.page.TableDataInfo;

/**
 * 联盟商家的二维码Controller
 *
 * @author gaolianjie
 * @date 2021-04-25
 */
@RestController
@RequestMapping("/union/qrcode")
public class UnionShopQrcodeController extends BaseController {
    @Autowired
    private IUnionShopQrcodeService unionShopQrcodeService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询联盟商家的二维码列表
     */
    @PreAuthorize("@ss.hasPermi('union:qrcode:list')")
    @GetMapping("/list")
    public TableDataInfo list(
            UnionShopQrcode unionShopQrcode,
            @RequestParam(name = "shopName", required = false) String shopName,
            @RequestParam(name = "startTime", required = false) String startTime,
            @RequestParam(name = "endTime", required = false) String endTime
    ) {
        startPage();

        Map<String, Object> time = new HashMap<>();
        time.put("startTime", startTime);
        time.put("endTime", endTime);

        List<Map<String, Object>> list = unionShopQrcodeService.selectUnionShopQrcodeList(unionShopQrcode, shopName, time);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('union:qrcode:downloadList')")
    @GetMapping("/downloadList")
    public AjaxResult downloadList(
            @RequestParam(name = "pageNum", required = false) Long pageNum,
            @RequestParam(name = "pageSize", required = false) Long pageSize
    ) {
        return AjaxResult.success(unionShopQrcodeService.getDownloadListForRedis(pageNum, pageSize));
    }

    @PreAuthorize("@ss.hasPermi('union:qrcode:createQr')")
    @PostMapping("/createQr")
    public AjaxResult createQr(@RequestBody Map<String, Object> params) {
        return toAjax(unionShopQrcodeService.createQr(
                params,
                tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId()));
    }
}
